﻿using PipeDetection.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PipeDetection.UI
{
    internal class DrawHelper
    {

        public static List<Point> GetPoints(DefectCatalog d, PictureBox picture)
        {
            var dh = new DrawHelper(picture);
            //鼠标拾取
            var q1 = dh.GetGeomerty(d);
            //还原图像坐标
            q1 = Common.ScalePoint(q1, picture);
            dh.Destory();
            return q1;
        }
        public static List<Point> GetPoints(Gtype gtype, PictureBox picture)
        {
            var dh = new DrawHelper(picture);
            //鼠标拾取
            var q1 = dh.GetGeomerty(gtype);
            //还原图像坐标
            q1 = Common.ScalePoint(q1, picture);
            dh.Destory();
            return q1;
        }


        private PictureBox pictureBox;
        private List<Point> points = new List<Point>();
        private bool isDrawing = false;
        
        private DefectCatalog defect;

        public DrawHelper(PictureBox box)
        {
            pictureBox = box;
            pictureBox.Visible = true;
            pictureBox.Capture = true;
            Cursor.Clip = pictureBox.RectangleToScreen(pictureBox.ClientRectangle);

            try
            {
                pictureBox.MouseDown -= PictureBox_MouseDown;
                pictureBox.MouseMove -= PictureBox_MouseMove;
                pictureBox.MouseUp -= PictureBox_MouseUp;
               pictureBox.Paint -= PictureBox_Paint;

            }
            catch (Exception)
            {
            }
            
            pictureBox.MouseDown += PictureBox_MouseDown;
            pictureBox.MouseMove += PictureBox_MouseMove;
            pictureBox.MouseUp += PictureBox_MouseUp;
            pictureBox.Paint += PictureBox_Paint;
        }
        /// <summary>
        /// 鼠标拾取图形
        /// </summary>
        /// <param name="gt"></param>
        /// <returns></returns>
        public   List<Point> GetGeomerty(Gtype gt)
        {

          var   defect1 = new DefectCatalog
            {
                GeometryType = gt,
                ColorOfPorL = Color.Red,
                HatchPattern = Hpattern.斜线
            };
            return GetGeomerty(defect1);
            
        }
        //绘制时显示，根据定义显示样式
        public List<Point> GetGeomerty( DefectCatalog dx)
        {
                 defect = dx;
            while (true)
            {
                if (isDrawing == false && points.Count > 0)
                {
                    return points;
                }
                Application.DoEvents();
            }

        }
        public void Destory()
        {
            isDrawing = false;
            pictureBox.Capture = false;
            Cursor.Clip = Rectangle.Empty;
            pictureBox.Visible = false;
            pictureBox.MouseDown -= PictureBox_MouseDown;
            pictureBox.MouseMove -= PictureBox_MouseMove;
            pictureBox.MouseUp -= PictureBox_MouseUp;
            pictureBox.Paint -= PictureBox_Paint;

        }

        private void PictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(Pens.Cyan, 0, curMouse.Y, pictureBox.Width, curMouse.Y);
            e.Graphics.DrawLine(Pens.Cyan, curMouse.X, 0, curMouse.X, pictureBox.Height);
            if (points.Count <= 1) return;

            Common.DrawSymbol(defect,e.Graphics, points);

        }
      

        private void PictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                isDrawing = false;
                pictureBox.Capture = false;
                Cursor.Clip = Rectangle.Empty;
                if(points.Count > 0) {
                    points.RemoveAt(points.Count - 1);
                    pictureBox.Invalidate();
                } 
            }
        }
        private Point curMouse;
        private void PictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            curMouse = e.Location;
            if (isDrawing)
            {
                points[points.Count-1]=e.Location;
            }
            pictureBox.Invalidate();
        }
        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (!isDrawing)
            {
                if (e.Button == MouseButtons.Left)
                {
                   
                    points.Clear();
                    isDrawing = true;
                    points.Add(e.Location);
                    points.Add(e.Location);
                }
            }
            else
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (defect.GeometryType == Gtype.点)
                    {
                        points[0] = e.Location;
                    }
                    else
                    {
                        points[points.Count - 1] = e.Location;
                        points.Add(e.Location);
                    }
                 
                }
            }
        }

       
    }
}
